﻿using ERPPublic.eneity;
using MediatR;
using PersonnelManagement.Application.Command.ManpowerCommand;
using PersonnelManagement.Domain.Dto;
using PersonnelManagement.Domain.PersonnelDomain;
using PersonnelManagement.Infrstructre.Interface;
using SqlSugar;

namespace PersonnelManagement.Application.CommandHandler.ManpowerCommandHandler
{
    public class FanManpowerCommandHandler : IRequestHandler<FanManpowerCommand, List<ListManpowerDto>>
    {

        IManpowerRepostiory mRep;
        ISqlSugarClient db;

        public FanManpowerCommandHandler(IManpowerRepostiory mRep, ISqlSugarClient db)
        {
            this.mRep = mRep;
            this.db = db;
        }

        public async Task<List<ListManpowerDto>> Handle(FanManpowerCommand request, CancellationToken cancellationToken)
        {
            var list = db.Queryable<Manpower>()
                .InnerJoin<Sector>((m, s) => m.SectorId == s.SectorId)
                .InnerJoin<Post>((m, s, p) => s.SectorId == p.SectorId)
                .Where(m => m.IsDel == false && m.ManpowerId == request.ManpowerId)
                .Select((m, s, p) => new ListManpowerDto
                {
                    ManpowerId = m.ManpowerId,
                    MNumber = m.MNumber,
                    MName = m.MName,
                    Users = m.Users,
                    MSex = m.MSex,
                    MPhone = m.MPhone,
                    MEmail = m.MEmail,
                    MIdCard = m.MIdCard,
                    SectorId = m.SectorId,
                    PostId = m.PostId,
                    MBirthDay = m.MBirthDay,
                    MWeChat = m.MWeChat,
                    MQQ = m.MQQ,
                    MContact = m.MContact,
                    MCPhone = m.MCPhone,
                    MPermanent = m.MPermanent,
                    MResidence = m.MResidence,
                    MIdPhoto = m.MIdPhoto,
                    MEntryDate = m.MEntryDate,
                    SectorName = s.SectorName,
                    PostName = p.PostName,
                }).ToList().DistinctBy(m => m.ManpowerId);
            return list.ToList();

        }
    }
}
